package com.weixin.project.Util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SignatureGenerator {
    public static String generate(String user_id, long timestamp, String nonce, String secret_key) throws NoSuchAlgorithmException {
        String str = user_id + timestamp + nonce + secret_key;
        MessageDigest md = MessageDigest.getInstance("SHA-1");
        md.update(str.getBytes());
        byte[] digest = md.digest();
        StringBuilder sb = new StringBuilder();
        for (byte b : digest) {
            sb.append(String.format("%02x", b & 0xff));
        }
        return sb.toString();
    }

    // 示例用法
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String user_id = "123456";
        long timestamp = System.currentTimeMillis() / 1000;
        String nonce = "foobar";
        String secret_key = "abcdefg";
        String signature = generate(user_id, timestamp, nonce, secret_key);
        System.out.println(signature);
    }
}
